package org.jaronsource.framework.plugins.dictionary.web.taglib;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.jaronsource.framework.plugins.dictionary.DictionaryHelper;
import org.jaronsource.framework.plugins.dictionary.domain.Dictionary;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class DictSelectTag extends TagSupport {

	private static final long serialVersionUID = 7814566879067987936L;
	private String dictType;
	private String field;
	private String id;
	private String defaultVal;
	private String cssClass;
	private boolean useHeader;
	private String headerLabel;
	private String properties;

	public int doStartTag() throws JspTagException {
		return EVAL_PAGE;
	}

	public int doEndTag() throws JspTagException {
		try {
			JspWriter out = this.pageContext.getOut();
			out.print( end().toString() );
		}
		catch ( IOException e ) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	public StringBuffer end() {
		
		this.defaultVal = this.defaultVal == null ? "" : this.defaultVal;
		this.headerLabel = this.headerLabel == null ? "" : this.headerLabel;
		
		ServletContext sc = pageContext.getServletContext();
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext( sc );

		DictionaryHelper dictionaryHelper = wac.getBean( DictionaryHelper.class );
		Map<String, Dictionary> dictionaries = dictionaryHelper.getDictionariesByType( dictType );
		
		
		StringBuffer sb = new StringBuffer();

		sb.append( "<select name=\"" + field + "\"" );
		if ( !StringUtils.isBlank( this.id ) ) {
			sb.append( " id=\"" + id + "\"" );
		}
		if ( !StringUtils.isBlank( this.cssClass ) ) {
			sb.append( " class=\"" + cssClass + "\"" );
		}
		if ( !StringUtils.isBlank( this.properties ) ) {
			sb.append( " " + properties );
		}
		
		sb.append( ">" );
		
		if (this.useHeader) {
			if ( "".equals( this.defaultVal ) ) {
				sb.append( " <option value=\"\" selected=\"selected\">" );
			}
			else {
				sb.append( " <option value=\"\">" );
			}
			sb.append( this.headerLabel );
			sb.append( " </option>" );
		}
		
		List<Dictionary> tmpList = new ArrayList<Dictionary>(dictionaries.values());
		Collections.sort( tmpList, new Comparator<Dictionary>() {

			@Override
			public int compare( Dictionary o1, Dictionary o2 ) {
				return o1.getRecordId().compareTo( o2.getRecordId() );
			}
			
		} );
		for ( Dictionary dict : tmpList ) {
			if ( dict.getDictKey().equals( this.defaultVal ) ) {
				sb.append( " <option value=\"" + dict.getDictKey() + "\" selected=\"selected\">" );
			}
			else {
				sb.append( " <option value=\"" + dict.getDictKey() + "\">" );
			}
			sb.append( dict.getDictValue() );
			sb.append( " </option>" );
		}

		sb.append( "</select>" );

		return sb;
	}

	public String getDictType() {
		return dictType;
	}

	public void setDictType( String dictType ) {
		this.dictType = dictType;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass( String cssClass ) {
		this.cssClass = cssClass;
	}

	public String getId() {
		return id;
	}

	public void setId( String id ) {
		this.id = id;
	}

	public String getDefaultVal() {
		return defaultVal;
	}

	public void setDefaultVal( String defaultVal ) {
		this.defaultVal = defaultVal;
	}

	public String getField() {
		return field;
	}

	public void setField( String field ) {
		this.field = field;
	}
	
	public boolean isUseHeader() {
		return useHeader;
	}
	
	public void setUseHeader( boolean useHeader ) {
		this.useHeader = useHeader;
	}

	public String getHeaderLabel() {
		return headerLabel;
	}

	public void setHeaderLabel( String headerLabel ) {
		this.headerLabel = headerLabel;
	}

	public String getProperties() {
		return properties;
	}

	public void setProperties( String properties ) {
		this.properties = properties;
	}
	
}
